package com.linran.structure_algorithm.数据结构.lesson8_树;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 二叉树
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();

        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");

        //说明，我们先手动创建该二叉树，后面我们学习递归的方式创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);

        //测试
//        System.out.println("前序遍历");// 1, 2, 3, 5, 4
//        binaryTree.preOrder();
//        System.out.println("中序遍历");
//        binaryTree.infixOrder(); // 2 1 5 3 4
//        System.out.println("后续遍历");
//        binaryTree.postOrder(); // 2 5 4 3 1

        System.out.println("前序遍历查询");
        // 前序比较4次
//        HeroNode heroNode = binaryTree.preOrderSearch(5);
        // 中序比较3次
//        HeroNode heroNode = binaryTree.infixOrderSearch(5);
        // 后续比较两次
//        HeroNode heroNode = binaryTree.postOrderSearch(5);

//        System.out.println("查询到节点"+ heroNode.toString());


        binaryTree.delete(2);
        binaryTree.postOrder();
    }


}

@Setter
class BinaryTree {
    private HeroNode root;

    public void preOrder() {
        if (root == null) {
            System.out.println("二叉树为空无法遍历");
            return;
        }
        root.preOrder();
    }

    public void infixOrder() {
        if (root == null) {
            System.out.println("二叉树为空无法遍历");
            return;
        }
        root.infixOrder();
    }

    public void postOrder() {
        if (root == null) {
            System.out.println("二叉树为空无法遍历");
            return;
        }
        root.postOrder();
    }

    // 前序遍历查找
    public HeroNode preOrderSearch(int no) {
        if (root == null) {
            System.out.println("二叉树为空无法遍历");
            return root;
        }
        return root.preOrderSearch(no);
    }

    // 中序遍历查找
    public HeroNode infixOrderSearch(int no) {
        if (root == null) {
            System.out.println("二叉树为空无法遍历");
            return root;
        }
        return root.infixOrderSearch(no);
    }

    // 后序遍历查找
    public HeroNode postOrderSearch(int no) {
        if (root == null) {
            System.out.println("二叉树为空无法遍历");
            return root;
        }
        return root.postOrderSearch(no);
    }

    // 删除子节点
    public void delete(int no) {
        if (root != null && root.getNo() == no) {
            root = null;
            return;
        }
        root.delete(no);
    }
}

@Setter
@Getter
@ToString(exclude = {"left", "right"})
class HeroNode {
    private int no;
    private String name;
    private HeroNode left; // 默认null
    private HeroNode right; // 默认null

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    // 前序遍历
    public void preOrder() {
        // 输出当前节点
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    // 中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        //输出当前节点
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    // 后序遍历
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        // 输出当前节点
        System.out.println(this);
    }

    // 前序遍历查找
    public HeroNode preOrderSearch(int no) {
        System.out.println("前序比较");
        if (this.getNo() == no) {
            return this;
        }
        HeroNode heroNode = null;
        if (this.getLeft() != null) {
            heroNode = this.getLeft().preOrderSearch(no);
            if (heroNode != null) {
                return heroNode;
            }
        }
        if (this.getRight() != null) {
            heroNode = this.getRight().preOrderSearch(no);
        }
        return heroNode;
    }

    // 中序遍历查找
    public HeroNode infixOrderSearch(int no) {
        HeroNode heroNode = null;
        if (this.getLeft() != null) {
            heroNode = this.getLeft().infixOrderSearch(no);
            if (heroNode != null) {
                return heroNode;
            }
        }
        System.out.println("中序比较");
        if (this.getNo() == no) {
            return this;
        }
        if (this.getRight() != null) {
            heroNode = this.getRight().infixOrderSearch(no);
        }
        return heroNode;
    }

    // 后序遍历查找
    public HeroNode postOrderSearch(int no) {
        HeroNode heroNode = null;
        if (this.getLeft() != null) {
            heroNode = this.getLeft().postOrderSearch(no);
            if (heroNode != null) {
                return heroNode;
            }
        }
        if (this.getRight() != null) {
            heroNode = this.getRight().postOrderSearch(no);
            if (heroNode != null) {
                return heroNode;
            }
        }
        System.out.println("后序比较");
        if (this.getNo() == no) {
            return this;
        }
        return heroNode;
    }

    public boolean delete(int no) {
        // 当前左子节点判断是否为目标删除节点
        if (this.getLeft() != null && this.getLeft().getNo() == no) {
            this.setLeft(null);
            return true;
        }
        // 当前右子节点判断是否为目标删除节点
        if (this.getRight() != null && this.getRight().getNo() == no) {
            this.setRight(null);
            return true;
        }
        // 左递归删除
        if (this.getLeft() != null && this.getLeft().delete(no)) {
            return true;
        }
        // 右递归删除
        if (this.getRight() != null && this.getRight().delete(no)) {
            return true;
        }
        return false;
    }
}
